//
//  AddFoldViewController.swift
//  EnglishDictionary
//
//  Created by mac on 2024/3/3.
//

import UIKit

class AddFoldViewController: UIViewController {

    @IBOutlet weak var inputTextField: UITextField!
    @IBOutlet weak var subTextField: UITextField!
    @IBOutlet weak var finishButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "新增文件夹" 
        
        self.inputTextField.layer.cornerRadius = 10
        self.inputTextField.clipsToBounds = true
        self.inputTextField.layer.borderWidth = 1
        self.inputTextField.layer.borderColor = UIColor.black.cgColor
        
        self.subTextField.layer.cornerRadius = 10
        self.subTextField.clipsToBounds = true
        self.subTextField.layer.borderWidth = 1
        self.subTextField.layer.borderColor = UIColor.black.cgColor
        
        self.finishButton.layer.cornerRadius = 10
        self.finishButton.clipsToBounds = true
        
//        self.inputTextField.text = "22年联考词汇"
//        self.subTextField.text = "Words"
    }
    
    @IBAction func finish(_ sender: Any) {
        let al = UIAlertController.init(title: "提示", message:"确认是否添加", preferredStyle: .alert)
        let ac_cancel = UIAlertAction.init(title: "取消", style:  .cancel){_ in
         }
        let ac_confirm = UIAlertAction.init(title: "确定", style:  .destructive){_ in
            if self.inputTextField.text?.count == 0 {
                TRHUD.showSuccess("请输入你要添加的文件夹名称")
                return
            }
            
            if self.subTextField.text?.count == 0 {
                TRHUD.showSuccess("请输入你要添加到的父级文件夹名称")
                return
            }
            
            let fold = Fold()
            fold.fold = self.inputTextField.text
            fold.parentFold = self.subTextField.text
            try?fold .saveOrUpdate()
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                TRHUD.showSuccess("添加成功")
                self.navigationController?.popViewController(animated: true)
            }
        }
        al.addAction(ac_cancel)
        al.addAction(ac_confirm)
        self.present(al, animated: true)
    }
}
